package com.javastar.modules.security.handler;

import com.javastar.common.exception.ErrorCode;
import com.javastar.common.result.Result;
import com.javastar.common.utils.ResponseUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限认证异常处理器
 *
 * @author zxx
 * @date 2023/7/26
 */
@Component
public class SysAccessDeniedHandler implements AccessDeniedHandler {
    /**
     * 返回权限校验失败错误信息
     * @param request that resulted in an <code>AccessDeniedException</code>
     * @param response so that the user agent can be advised of the failure
     * @param accessDeniedException that caused the invocation
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseUtil.write(response, new Result().error(ErrorCode.ACCESS_DENIED_ERROR));
    }
}
